﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
//using System.Runtime.InteropServices.CustomMarshalers;
using System.Reflection;


using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;

using SwAddinHelpers.Contracts;
using SwAddinHelpers;
using SwAddinHelpers.Base;
using SwAddinHelpers.Attributes;
using SwAddinHelpers.Sinks;
using SwAddinHelpers.ComHelpers;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.ComponentModel.Composition.Hosting;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic;
//using Microsoft.VisualBasic.CompilerServices;

namespace ExternalControls
{
    [ComVisible(true)]
    [ProgId(PROGID)]
    [Export(typeof(IExternalUserControl))]
    [HostedAsFeatureView("","External Feature View",SolidWorks.Interop.swconst.swFeatMgrPane_e.swFeatMgrPaneTop, 0.5, true,true,true)]
    public partial class FeatureViewControl1: UserControlBase, IExternalUserControl
    {
        public const string PROGID = "SwExternalControls.ExternalFeatureViewControl1";

        public dynamic ExternalUserControl
        {
            get { return this; }
        }

        public FeatureViewControl1()
        {
            InitializeComponent();
        }

        public override void Tie()
        {
            _OwnerHost.SubscribeEvent(enumEventSink.Document_NewSelectionNotify, enumEventSubscriptionSource.GUI_CONTROL);
        }

        public override void Untie()
        {
            _OwnerHost.UnsubscribeEvent(enumEventSink.Document_NewSelectionNotify, enumEventSubscriptionSource.GUI_CONTROL);
        }

        [SinkMethod(enumEventSink.Document_NewSelectionNotify)]
        private void NewSelectionNotification(object sender, SolidWorksEventArgs e)
        {
            ModelDoc2 doc = ((ModelDoc2)_OwnerHost.OwnerAddin.SolidWorksApplication.IActiveDoc2);
            SelectionMgr selectionMgr = null;

            if (doc != null)
            {
                selectionMgr = doc.SelectionManager;
                int selectedObjectCount = selectionMgr.GetSelectedObjectCount2(-1);

                // needs elaborate SWITCH based on selection type.

                if (selectedObjectCount == 1)
                {
                    swSelectType_e selectionType = (swSelectType_e)selectionMgr.GetSelectedObjectType3(1, -1);
                        
                    //retrieve feature expecting type with GetFeature()
                    dynamic o = selectionMgr.GetSelectedObject6(1, -1);

                    //IntPtr ptr = Marshal.GetITypeInfoForType(o.GetType()); // fails since no Interop
                    //IDispatch dispatch = o as IDispatch;
                    //ITypeInfo iType = (ITypeInfo)Marshal.GetTypedObjectForIUnknown(ptr, typeof(ITypeInfo));
                    //Marshal.Release(ptr);
                    
                     //ComObject co = new ComObject(o);
                     

                        string name = null;

                        // Dimension
                        try
                        {
                            Dimension d = o.GetDimension2(0);
                            name = d.FullName;
                            Marshal.ReleaseComObject(d);
                        }
                        catch { }

                        // ?
                        if (name == null)
                        {
                            try
                            {
                                name = o.Name;
                            }
                            catch { }
                        }

                        // Feature from component
                        if (name == null)
                        {
                            try
                            {
                                Feature f = o.GetFeature();
                                name = f.Name;
                                Marshal.ReleaseComObject(f);
                            }
                            catch { }
                        }
                    

                        if (name == null)
                            name = "Selection Type Not Supported";

                        /* Old School
                        Face2 o = (Face2)selectionMgr.GetSelectedObject6(1, -1);
                        Feature f = (Feature)o.GetFeature();
                        Feature f = (Feature)RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(selectionMgr.GetSelectedObject6(1, -1), null, "GetFeature", new object[0], null, null, null));
                        */

                        txtCurrentSelection.Text =  name;
                        txtCurrentSelectionType.Text = selectionType.ToString();

                        

                        if (o != null) Marshal.ReleaseComObject(o);
                }

                Marshal.ReleaseComObject(selectionMgr);
                Marshal.ReleaseComObject(doc);
            }
        }
    }
}


